package com.glodon.ggzy.fwpt.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.JsonUtils;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.Tools;
import com.glodon.ggzy.entity.GgJsTenderNotice;
import com.glodon.ggzy.fwpt.service.IJsgcProjectService;
import com.glodon.ggzy.vo.JsgcInfoVO;

/**
 * 
 * 行政处理控制器
 * @author yangjf
 * @date 2013/06/11
 */
@Controller
public class XzclController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IJsgcProjectService jsgcProjectService;

	/**
	 * 获取行政处理集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_list")
	public String getXzclList(Model model) {
		return "/fwpt/xzcl_list";
	}

	/**
	 * 
	 * 行政处理-建设工程 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_jsgc_page_list")
	public String xzclJsgcPageList(Model model) {
		return "/fwpt/xzcl_jsgc_page_list";
	}

	/**
	 * 
	 * 行政处理-建设工程 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_tdfc_page_list")
	public String xzclTdfcPageList(Model model) {
		return "/fwpt/xzcl_tdfc_page_list";
	}

	/**
	 * 
	 * 行政处理-建设工程 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_cqjy_page_list")
	public String xzclCpjyPageList(Model model) {
		return "/fwpt/xzcl_cqjy_page_list";
	}

	/**
	 * 
	 * 行政处理-建设工程 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_zfcg_page_list")
	public String xzclZfcgPageList(Model model) {
		return "/fwpt/xzcl_zfcg_page_list";
	}

	/**
	 * 
	 * 行政处理-建设工程 分页数据加载
	 * @param model
	 * @param rows
	 * @param page
	 * @param response
	 */
	@RequestMapping("/fwpt/zxcl_jsgc_page_data")
	public void getZbgg_JsgcPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		Page queryPage = jsgcProjectService.queryJsgcXzclInfo(qm, searchText);
		List<GgJsTenderNotice> noticeList = (List<GgJsTenderNotice>) queryPage.getResult();
		List<JsgcInfoVO> xzclList = new ArrayList<JsgcInfoVO>();
		for (GgJsTenderNotice notice : noticeList) {
			JsgcInfoVO xzcl = new JsgcInfoVO(notice.getId(), notice.getProjectName(), notice.getNoticeType(), Tools.formatDateToString(notice
					.getApplyStartTime()), notice.getId());
			xzclList.add(xzcl);
		}
		queryPage.setResult(xzclList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取行政处理-建设工程集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_jsgc_list")
	public String getXzclJsgcList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/xzcl_jsgc_list";
	}

	/**
	 * 获取行政处理-建设工程详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_jsgc_detail")
	public String getXzclJsgcDetail(String id, Model model) {
		GgJsTenderNotice tenderNotice = jsgcProjectService.getGgJsTenderNoticeById(id);
		model.addAttribute("tenderNotice", tenderNotice);
		if (tenderNotice != null && !StringUtils.isEmpty(tenderNotice.getId())) {
			model.addAttribute("noticeId", tenderNotice.getId());
		}
		return "/fwpt/xzcl_jsgc_detail";
	}

	/**
	 * 获取行政处理-政府采购集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_zfcg_list")
	public String getXzclZfcgList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/xzcl_zfcg_list";
	}

	/**
	 * 获取行政处理-政府采购详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_zfcg_detail")
	public String getXzclZfcgDetail(Integer prjId, Model model) {
		model.addAttribute("detail", null);
		return "/fwpt/xzcl_zfcg_detail";
	}

	/**
	 * 获取行政处理-产权交易集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_cqjy_list")
	public String getXzclCqjyList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/xzcl_cqjy_list";
	}

	/**
	 * 获取行政处理-产权交易详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_cqjy_detail")
	public String getXzclCqjyDetail(Integer prjId, Model model) {
		model.addAttribute("detail", null);
		return "/fwpt/xzcl_cqjy_detail";
	}

	/**
	 * 获取行政处理-土地房产集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_tdfc_list")
	public String getXzclTdfcList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/xzcl_tdfc_list";
	}

	/**
	 * 获取行政处理-土地房产详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/xzcl_tdfc_detail")
	public String getXzclTdfcDetail(Integer prjId, Model model) {
		model.addAttribute("detail", null);
		return "/fwpt/xzcl_tdfc_detail";
	}

	/**
	 * 
	 * 获取信息公开-建设工程-图片链接
	 * @param model
	 * @param id
	 * @param response
	 */
	@RequestMapping("/fwpt/xzcl_jsgc_img")
	public void getXxgkJsgcImg(Model model, String id, HttpServletResponse response) {
		GgJsTenderNotice tenderNotice = jsgcProjectService.getGgJsTenderNoticeById(id);
		OutputStream outStream;
		try {
			outStream = response.getOutputStream();
			outStream.write(tenderNotice.getNoticeImage(), 0, tenderNotice.getNoticeImage().length);
			outStream.flush();
			outStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
